package com.hp.es.elasticsearch.util;

import org.springframework.cglib.beans.BeanMap;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author hp
 * @Title 工具类
 * @Package
 * @Date 2021/9/28 17:15
 * @Description
 * @Version 1.0
 */
public class ObjectToMapUtils {
    /**
     * 将对象转换成Map
     * @param bean
     * @param <T>
     * @return
     */
    public static <T> Map<String,Object> beanToMap(T bean){
        Map<String,Object> map = new HashMap<>();
        if(bean != null){
            BeanMap beanMap = BeanMap.create(bean);
            for(Object key : beanMap.keySet()){
                if(beanMap.get(key) != null){
                    map.put(key + "",beanMap.get(key));
                }
            }
        }
        return map;
    }
}
